import 'dart:convert';

import 'package:lib_core/logger/gt_logger.dart';

/// Author: Shper
/// Created: 2023/7/9
class GTLiCoreEventModel {

  static const KEY_ACTION_TYPE = "actionType";
  static const KEY_ACTION_NAME = "actionName";

  // 枚举 类名
  String actionType = "";
  // 枚举 action 名
  String actionName = "";
  // 数据 Map
  Map<String, dynamic>? data;

  GTLiCoreEventModel();

  GTLiCoreEventModel.fromJson(dynamic json) {
    actionType = json[KEY_ACTION_TYPE] ?? "";
    actionName = json[KEY_ACTION_NAME] ?? "";
    if (json['dataMap'] != null) {
      data = json['dataMap'];
    } else if (json['dataJson'] != null) {
      data = jsonDecode(json['dataJson']);
    }
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['actionType'] = actionType;
    map['actionName'] = actionName;
    map['data'] = jsonEncode(data);
    return map;
  }

  bool isValid() {
    if (actionType.isEmpty) {
      GTLogger.debug("GTEventCenter: The actionType is invalid.");
      return false;
    }
    if (actionName.isEmpty) {
      GTLogger.debug("GTEventCenter: The actionName is invalid.");
      return false;
    }
    return actionType.isNotEmpty && actionName.isNotEmpty;
  }

  bool isInvalid() {
    return !isValid();
  }

}